package a20;

import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

public class A20 {
    public static void main(String[] args) throws Exception {
        AnnotationConfigServletWebServerApplicationContext context =
                new AnnotationConfigServletWebServerApplicationContext(WebConfig.class);

        RequestMappingHandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
        handlerMethods.forEach((k,v) -> {
            System.out.println(k + ">>>" + v);
        });

        //HandlerExecutionChain 执行链，包括Controller里的方法和拦截器
//        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test1");
//        MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test4");   //RequestMappingHandlerAdapter里含有参数解析器，解析@RequestParam等注解
        MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test5");   //RequestMappingHandlerAdapter里含有参数解析器，解析@RequestParam等注解
        request.addParameter("name", "tiger");
        request.addHeader("token", "这是一个token");
        MockHttpServletResponse response = new MockHttpServletResponse();
        HandlerExecutionChain chain = handlerMapping.getHandler(request);
        System.out.println(chain);


        System.out.println(">>>>>>>>>adapter调用方法");
        MyRequestMappingHandlerAdapter adapter = context.getBean(MyRequestMappingHandlerAdapter.class);
        adapter.invokeHandlerMethod(request, response, ((HandlerMethod) chain.getHandler()));
        byte[] bytes = response.getContentAsByteArray();
        System.out.println(new String(bytes, StandardCharsets.UTF_8));

        /**
        System.out.println(">>>>>>>>>>>参数解析器，解析注解比如：@@RequestParam、@RequestBody等");
        List<HandlerMethodArgumentResolver> argumentResolvers = adapter.getArgumentResolvers();
        for (HandlerMethodArgumentResolver resolver : argumentResolvers) {
            System.out.println(resolver);
        }

        System.out.println(">>>>>>>>>>>>>>>>>>>>> 所有返回值解析器");
        for (HandlerMethodReturnValueHandler handler : adapter.getReturnValueHandlers()) {
            System.out.println(handler);
        }
         **/
    }
}
